package Servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import utilidades.Escritor;
import utilidades.HtmlGen;
import utilidades.Validador;
import Inscripcion.Participante;
import Inscripcion.Usuario;

@WebServlet("/Modificar")
public class Modificar extends Principal{
	private static final long serialVersionUID = 1L;
	private ServletContext sc;

	public Modificar() {
		super();
	}   	
	
	public void init(ServletConfig config) throws ServletException{
		
		super.init(config);
		sc = getServletContext();
		
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}  	
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{		
			//Capturamos los parámetros 
			HttpSession session = request.getSession(true);
			
			Validador val = new Validador(sc);
			boolean respuesta = false;
			
			String uid 	= (String) session.getAttribute("user");
			String pass = (String) session.getAttribute("pass");
			
			//TODO Capturar error de validacion
			respuesta = val.validar(uid, pass);
			
			
			PrintWriter out;
			out = response.getWriter();
			   
			//Generamos el Html de modificación de datos de usuario
			response.setContentType("text/html");
			
			out.println("<html>");
			out.println("<head><title>Enviar parametros a un Servlet</title></head>");
			
			String urlEstilo = request.getContextPath() + "/css/cv.css"; 
			out.println("<link rel=\"stylesheet\" href=" + urlEstilo + ">");
			out.println("<meta http-equiv=\"content-type\" lang=\"es\" " +
					"content=\"text/html; charset=utf-8\"/>");

			out.println("</head>");
			out.println("<body>");
			
			if(respuesta){
				HtmlGen.escribeCabeceraLogin(out);
				
				//TODO Extraer la información del usuario
				
				HashMap<String, Usuario> datosRegistro = Escritor.getRegistrados(sc);
				
				Usuario usr = datosRegistro.get(uid);
				
				out.println("<div id=\"contenedor\">");
					out.println("<div id=\"inscripcion\" class=\"apartado_cv\">");
						out.println("<h2 class=\"title\"> Modificación datos usuario</h2>");
						
						out.println("<p>");
		
						out.println("<form action=\"http://localhost:8080/Practica4Pawii/Principal\" method=\"post\">");
							out.println("<input type=\"hidden\" name=\"accion\" value=\"cambioInfoUser\" >");
							
								out.println("<p>Nombre:</p>");
								out.println("<p><input type=\"text\" name=\"nombre\" size=\"50\" value=\""+ usr.getNombre()+ "\" " +
										"required=\"required\"></p>");
								
								out.println("<p>Primer Apellido:</p>");
								out.println("<p><input type=\"text\" name=\"apellido1\" size=\"50\" value=\""+ usr.getApellido1()+ "\"" +
										"required=\"required\"></p>");
								
								out.println("<p>Segundo Apellido:</p>");
								out.println("<p><input type=\"text\" name=\"apellido2\" size=\"50\" value=\""+ usr.getApellido2()+ "\"" +
										"required=\"required\"></p>");
								
								out.println("<p>Correo electrónico:</p>");
								out.println("<p><input type=\"text\" name=\"mail\" size=\"50\" value=\""+ usr.getCorreo()+ "\"" +
										"required=\"required\"></p>");
								
	//							out.println("<p>Username:</p>");
	//							out.println("<p><input type=\"text\" name=\"username\" size=\"50\" value=\""+ usr.getUsername()+ "\"" +
	//									"required=\"required\"></p>");
								
								out.println("<p>Password:</p>");
								out.println("<p><input type=\"password\" value=\""+ usr.getPassword()+ "\" name=\"password\" size=\"50\" " +
										"required=\"required\"></p>");
								
								out.println("<p>Repeat Password:</p>");
								out.println("<p><input type=\"password\" value=\""+ usr.getPassword()+ "\" name=\"password2\" size=\"50\" " +
										"required=\"required\"></p>");
								
								out.println("<p>Sexo <br/>");
									out.println("<input type=\"radio\" name=\"sexo\" value=\"hombre\" checked=\"checked\" /> Hombre");
										out.println("<input type=\"radio\" name=\"sexo\" value=\"mujer\" /> Mujer");
									
									out.println("</p>");
												
											out.println("<p><input type=\"submit\" value=\"Modificar\"> ");
										out.println("</form>");
										
								out.println("<form action=\"http://localhost:8080/Practica4Pawii/Principal\" method=\"post\">");
									out.println("<input type=\"hidden\" name=\"accion\" value=\"baja\" >");
									out.println("<input class=\"btnLink\" type=\"submit\" value=\"Baja Usuario\">");
								out.println("</form>");
										
									out.println("</p>");
	
							out.println("</p>");
						out.println("</p>");
						
						out.println("</div>");
					out.println("</div>");
			}
			else{

				response.setHeader("Cache-Control", "no-cache, no-store");
				response.setHeader("Pragma", "no-cache"); 
							 
				response.sendRedirect(request.getContextPath() + "/ErrorPages/accesDenied.html");
			}
		}
		catch(Exception e){
			response.setHeader("Cache-Control", "no-cache, no-store");
			response.setHeader("Pragma", "no-cache"); 
						 
			response.sendRedirect(request.getContextPath() + "/ErrorPages/unknow.html");
		}
	}
}

